﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CIRDiagnoseDevice.DBManage;

namespace CIRDiagnoseDevice.TestConfig
{
    /// <summary>
    /// 通过测试的组，测试名称通用的判定是否合格类
    /// </summary>
    class TongYongPanDing : IJudgeTestValue
    {

        private string testgroup;
        private string testname;
        public TestRecordManage trm;
        public TongYongPanDing( TestRecordManage trm)
        {
            this.trm = trm; 
        }

        #region IJudgeTestValue 成员

        public bool Judge(float  value)
        {
            bool judgeresult = false;
            StringBuilder sb = new StringBuilder();
            sb.Append("select  * from tb_TestIndexConfig tic where  ");
            //如果没有下限那么取传入的测试值(下一句)，就可以使其判定为true了
            sb.Append(" case when ");
            sb.Append(" case when defaultConfig=1 then  tic.minimum else tic.userminimum end ");//如果现在是要求取系统默认配置值则取minimum否则取userminimum
            sb.Append(" is null then "); 
            sb.Append(value.ToString());
            sb.Append(" * power(10,CASE WHEN exponent<0 THEN exponent ELSE 0 END )  else ");//由于power函数不能接受负数，因此需要判定正负以确定是在数据库的标准乘以次方还是测试值乘以次方
            sb.Append(" case when defaultConfig=1 then  tic.minimum else tic.userminimum end ");//如果现在是要求取系统默认配置值则取minimum否则取userminimum
            sb.Append("  end * power(10,CASE WHEN exponent>0 THEN exponent ELSE 0 END ) <= ");//如果不为空则取数据库的标准值且要乘以次方
            sb.Append(value.ToString());
            sb.Append(" * power(10,CASE WHEN exponent<0 THEN exponent ELSE 0 END ) and ");//以上拼起来就是测试值不能比最小值小
            sb.Append(" case when ");
            sb.Append(" case when defaultConfig=1 then  tic.maximum else tic.usermaximum end ");//如果现在是要求取系统默认配置值则取minimum否则取userminimum
            sb.Append(" is null then ");
            sb.Append(value.ToString());
            sb.Append(" * power(10,CASE WHEN exponent<0 THEN exponent ELSE 0 END )  else ");
            sb.Append(" case when defaultConfig=1 then  tic.maximum else tic.usermaximum end ");//如果现在是要求取系统默认配置值则取minimum否则取userminimum
            sb.Append(" end * power(10,CASE WHEN exponent>0 THEN exponent ELSE 0 END )>= ");
            sb.Append(value.ToString());
            sb.Append(" * power(10,CASE WHEN exponent<0 THEN exponent ELSE 0 END ) and TestGroup= N'");
            sb.Append(testgroup.ToString());
            sb.Append("' and TestName like N'%");
            sb.Append(testname.ToString() + "%'");
            if (trm.ExecuteSqlScalar(sb.ToString()) != null)
            {
                judgeresult = true;
            }
            return judgeresult;
        }
        /// <summary>
        /// 由于经常是测试分类已经确定了然后根据具体的测试和值进行判定，为了不用先赋值后调用方法，加了此方法
        /// </summary>
        /// <param name="testname"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public bool JudgeByTestName(string testname, float value)
        {
            this.testname = testname;
            bool judgeresult = false;
            StringBuilder sb = new StringBuilder();
            sb.Append("select  * from tb_TestIndexConfig tic where  ");
            sb.Append("case when tic.minimum is null then ");//如果没有下限那么取传入的测试值(下一句)，就可以使其判定为true了
            sb.Append(value.ToString());
            sb.Append(" * power(10,CASE WHEN exponent<0 THEN exponent ELSE 0 END ) ");//由于power函数不能接受负数，因此需要判定正负以确定是在数据库的标准乘以次方还是测试值乘以次方
            sb.Append(" else tic.minimum end * power(10,CASE WHEN exponent>0 THEN exponent ELSE 0 END ) <= ");//如果不为空则取数据库的标准值且要乘以次方
            sb.Append(value.ToString());
            sb.Append(" * power(10,CASE WHEN exponent<0 THEN exponent ELSE 0 END ) and ");//以上拼起来就是测试值不能比最小值小
            sb.Append(" case when tic.maxnum is null then ");
            sb.Append(value.ToString());
            sb.Append(" * power(10,CASE WHEN exponent<0 THEN exponent ELSE 0 END ) ");
            sb.Append("  else tic.maxnum end * power(10,CASE WHEN exponent>0 THEN exponent ELSE 0 END )>= ");
            sb.Append(value.ToString());
            sb.Append(" * power(10,CASE WHEN exponent<0 THEN exponent ELSE 0 END ) and TestGroup= N'");
            sb.Append(testgroup.ToString());
            sb.Append("' and TestName= N'");
            sb.Append(testname.ToString() + "'");
            if (trm.ExecuteSqlScalar(sb.ToString())!=null)
            {
                judgeresult = true;
            }
            return judgeresult;
        }
        /// <summary>
        /// 对应数据库的此字段，代表测试的组（分类）
        /// </summary>
        public string TestGroup
        {
            get
            {
                return testgroup;
            }
            set
            {
                testgroup = value;
            }
        }
        /// <summary>
        /// 对应数据库的测试项目
        /// </summary>
        public string TestName
        {
            get
            {
                return testname;
            }
            set
            {
                testname = value;
            }
        }

        #endregion
    }
}
